package xf.tools;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;

public class Loading {

	private static final String TAG = "Loading";

	/** 加载数据对话框 */
	private static Dialog mLoadingDialog;

	private static MyCount mc;

	static TextView loadingText;

	/**
	 * 显示加载对话框
	 * 
	 * @param context
	 * 
	 * @param msg
	 *            对话框显示内容
	 * @param cancelable
	 *            对话框是否可以点击取消 true 可以 false 不能
	 * @param btn
	 *            对话框是否可以点back按键取消 true 可以 false 不能
	 * @param count
	 *            超时时间
	 */
	@SuppressLint("InflateParams")
	public static void showDialogForLoading(Context context, String msg, boolean cancelable, final boolean btn,
			long count) {
		hideDialogForLoading();
		View view = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, null);
		loadingText = (TextView) view.findViewById(R.id.id_tv_loading_dialog_text);
		loadingText.setText(msg);

		if (context == null) {
			Log.d(TAG, "context==null");
			return;
		}

		mLoadingDialog = new Dialog(context, R.style.loading_dialog_style);
		mLoadingDialog.setCancelable(cancelable);
		mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

		try {
			if (mLoadingDialog.isShowing()) {
				hideDialogForLoading();
			} else {
				mLoadingDialog.show();
			}
		} catch (NullPointerException e) {
			Log.d(TAG, "mLoadingDialog.isShowing:==//==>>空指针异常:--->" + e.getMessage());
		} catch (Exception e) {
			Log.d(TAG, "mLoadingDialog.isShowing:==//==>>" + e.getMessage() + "<<==\\==异常");
		}
		mLoadingDialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK && btn) {
					hideDialogForLoading();
					return true;
				}
				return false;
			}
		});
		mc = new MyCount(count, 1000);
		mc.start();
	}

	public static void SetTextViewContent(String str) {
		loadingText.setText(str);
	}

	/**
	 * 关闭加载对话框
	 */
	public static void hideDialogForLoading() {
		if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
			try {
				mLoadingDialog.cancel();
				mc.cancel();
			} catch (IllegalArgumentException e) {
				Log.v(TAG, "引用参数异常");
				e.printStackTrace();
			}
		}
	}

	/** 倒计时定时器 */
	static class MyCount extends CountDownTimer {
		public MyCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			hideDialogForLoading();
			mc.cancel();
		}

		@Override
		public void onTick(long millisUntilFinished) {
			Log.d(TAG, "请等待(" + millisUntilFinished / 1000 + ")秒...");
			if (millisUntilFinished / 1000 < 2) {
				Log.w(TAG, "已超时...");
				EmmApplication.T("操作超时");
			}
		}
	}

}